package iewShopApp.namespace;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class SineCalcActivity extends Activity {

	public GlobalVars Constants;
	public EditText gauge;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sine_calc_main);
		gauge = (EditText) findViewById(R.id.gaugeHeight);
//		gauge.setText("0");

		Constants = new GlobalVars();
		Constants.sineAngle = 30;
		Constants.sineLength = 5;
		calculate();

		final EditText userLength = (EditText) findViewById(R.id.userLength);
		final EditText userAngle = (EditText) findViewById(R.id.userAngle);
		userAngle.setText(String.valueOf(30));
		userLength.setText(String.valueOf(5));
		
		userLength.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button

				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press
					
					String strAngle = userAngle.getText().toString();
					if (Double.parseDouble(strAngle) > 90) {
						strAngle = String.valueOf(90);
						userAngle.setText(strAngle);
					}
					Constants.sineAngle = Double.parseDouble(strAngle);
					String strLength = userLength.getText().toString();
					Constants.sineLength = Double.parseDouble(strLength);
									
					calculate();
					// Force EditText from gaining focus all the bloody time
					userLength.clearFocus();
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(userLength.getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);

					return true;
				}
				return false;
			}
		});


		userAngle.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button

				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press
					String strAngle = userAngle.getText().toString();
					String strLength = userLength.getText().toString();
					if (Double.parseDouble(strAngle) > 90) {
						strAngle = String.valueOf(90);
						userAngle.setText(strAngle);
					}
					Constants.sineAngle = Double.parseDouble(strAngle);
					Constants.sineLength = Double.parseDouble(strLength);
					calculate();
					

					// Force EditText from gaining focus all the bloody time
					userAngle.clearFocus();
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(userAngle.getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);

					return true;
				}
				return false;
			}
		});

//	final EditText GaugeOutput = (EditText) findViewById(R.id.gaugeHeight);
	gauge.setOnKeyListener(new OnKeyListener() {
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			// If the event is a key-down event on the "enter" button

			if ((event.getAction() == KeyEvent.ACTION_DOWN)
					&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
				// Perform action on key press
				
				String strAngle = userAngle.getText().toString();
				String strLength = userLength.getText().toString();
				if (Double.parseDouble(strAngle) > 90) {
					strAngle = String.valueOf(90);
					userAngle.setText(strAngle);
				}
				Constants.sineAngle = Double.parseDouble(strAngle);
				Constants.sineLength = Double.parseDouble(strLength);
				calculate();
				

				// Force EditText from gaining focus all the bloody time
				gauge.clearFocus();
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(gauge.getWindowToken(),
						InputMethodManager.HIDE_NOT_ALWAYS);

				return true;
			}
			return false;
		}
	});

}

	void calculate() {
		gauge = (EditText) findViewById(R.id.gaugeHeight);
		double gaugeBlock = Math.round(1000
				* Math.sin(Math.toRadians(Constants.sineAngle))
				* Constants.sineLength) / 1000.;
		// gauge.setText(String.valueOf(gaugeBlock));
		gauge.setText(String.valueOf(gaugeBlock));
	}
}
